<?php

namespace Home\Controller;

/**
 * 房间ajax接口
 * @modify trainyao 2016-01-04 创建
 */
class RoomController extends \Home\Controller\ParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();

        //检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $hotel = I('post.hotel');
        $adminAuthorityLogic->check($hotel);
    }

    private $priceLogMap = array(
        'zh_cn' => array(
            'clean' => '清除尾房特卖金额',
            'price' => '价格由',
            'change' => '改为',
            'set' => '价格设置为',
            'dec' => '预定立减',
            'charity' => '义卖价',
        ),
        'en_us' => array(
            'clean' => 'clean the last room price ',
            'price' => 'price from ',
            'change' => ' to ',
            'set' => 'set price ',
            'dec' => 'minus ',
            'charity' => 'charity ',
        ),
        'zh_tw' => array(
            'clean' => '清除尾房特賣金額',
            'price' => '價格由',
            'change' => '改為',
            'set' => '價格設置为',
            'dec' => '預訂立減',
            'charity' => '義賣價',
        ),
    );

    /**
     * 添加房型
     * @modify trainyao 2016-01-04 创建
     * @modify zhangzhen 2017-01-10 添加rf卡判断
     */
    public function addRoom() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
            'price' => 'notnull,notempty,decimal8_2',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $roomNumberArray = empty($param['room']) ? array() : explode(',', $param['room']);
        $roomData = array();
        if (!empty($roomNumberArray)) {
            foreach ($roomNumberArray as $roomNumber) {
                $this->ajaxValidateReturn($this->validateUtil, $roomNumber, 'notnull,notempty,nameLength15');
                array_push($roomData, array('name' => $roomNumber, 'from' => 'mz'));
            }
        }

        $hotelId = $param['hotel'];
        // 检查是否是义卖客栈
        $result = $this->checkUsageHotel($hotelId);
        if ($result['isFreeHotel'] === true) {
            $this->ajaxValidateReturn($this->validateUtil, $param['charityPrice'], 'notnull,notempty,charityDecimal8_2');
        }

        $roomCount = count($roomData);
        if ($result['isFreeHotel'] === true && $result['existsRooms'] + $roomCount > MAXROOMS) {
            returnJson(403, 421); // 义卖客栈房间不能超过5间
        }
        // 义卖价小于房价
        if (isset($param['charityPrice']) && $param['charityPrice'] >= $param['price']) {
            returnJson(403, 425);
        }

        // 检查客栈是否有房间数量限制
        $checkResult = $this->checkHotelRoomLimit($hotelId, $roomCount);
        if ($checkResult === true) {
            returnJson(403, 318);
        }

        $roomTypeName = $param['name'];
        $roomTypePrice = $param['price'];
        $roomTypeCharityPrice = isset($param['charityPrice']) ? $param['charityPrice'] : null;

        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomTypeId = $roomTypeLogic->addInfo($hotelId, $roomTypeName, $roomTypePrice, $roomData, $roomTypeCharityPrice);

        returnJson(200, 200, $roomTypeId);
    }

    /**
     * 修改房型
     * @modify trainyao 2016-01-04    创建
     * @modify zhangzhen 2017-01-10 添加rf卡判断
     */
    public function editRoom() {
        $param = I('post.');

        $validateRule = array(
            'room' => 'notnull,notempty,id',
            'hotel' => 'notnull,notempty,id',
            'name' => 'notempty,nameLength15',
            'price' => 'notempty,decimal8_2',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        // 校验房间信息
        $rooms = json_decode(htmlspecialchars_decode($param['rooms']), true);
        if (!is_array($rooms)) {
            returnJson(403, 213);
        }
        $validateRule = array(
            'id' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength15',
        );
        // 新增房间数
        $roomCount = 0;
        foreach ($rooms as &$room) {
            $this->ajaxValidateReturn($this->validateUtil, $room, $validateRule);
            $room['from'] = 'mz';
            if ($room['id'] == 0) {
                 $roomCount ++;
            }
        }

        $hotelId = $param['hotel'];
        // 检查是否是义卖客栈 并且房间不能超过5间
        $result = $this->checkUsageHotel($hotelId);
        if ($result['isFreeHotel'] === true) {
            $this->ajaxValidateReturn($this->validateUtil, $param['charityPrice'], 'notnull,notempty,charityDecimal8_2');
        }
        if ($result['isFreeHotel'] === true && $result['existsRooms'] + $roomCount > MAXROOMS) {
            returnJson(403, 421); // 义卖客栈房间不能超过5间
        }
        // 义卖价小于房价
        if (isset($param['charityPrice']) && $param['charityPrice'] >= $param['price']) {
            returnJson(403, 425);
        }

        // 义卖价小于 打折后的分销价
        $roomTypeInfo = D('RoomType')->getInfo($param['room']);
        $discount = isset($roomTypeInfo['discount']) ? $roomTypeInfo['discount'] : null;
        if (isset($param['charityPrice']) && !is_null($discount) && $param['price'] * ($discount/10) <= $param['charityPrice']) {
            returnJson(403, 427);
        }

        // 房价打折后的分销价不能低于立减优惠金额
        $minPrice = $param['price'] - $param['charityPrice'];
        if (!is_null($discount) &&  $roomTypeInfo['minustype'] == 1 && $minPrice * ($discount/10) <= $roomTypeInfo['minusdiscount']) {
            returnJson(403, 314, 'minusdiscount error');
        }

        // 检查客栈是否有房间数量限制
        $checkResult = $this->checkHotelRoomLimit($hotelId, $roomCount);
        if ($checkResult === true) {
            returnJson(403, 318);
        }

        // (今天是否存在尾房特卖)
        $lastRoomPriceInfo = D('LastRoomPrice')->getInfoByRoomTypeDate($param['room'], time());
        $todayStartTime = date('H');         
        if (!empty($lastRoomPriceInfo) && $lastRoomPriceInfo['starttime'] <= $todayStartTime && $todayStartTime <= $lastRoomPriceInfo['endtime']) {
            // 义卖价小于尾房特卖
            if (isset($param['charityPrice']) && $param['price'] - $lastRoomPriceInfo['price'] <= $param['charityPrice']) {
                returnJson(403, 428);
            }
        }

        // 检查传过来的房间名是否重复
        if (count($rooms) != count(array_unique(array_column($rooms, 'name')))) {
            returnJson(403, 303, 'error');
        }

        // 判断房间是否锁掌柜房间
        $roomLogic = A('Common/Room', 'Logic');
        $qrmRoom = $roomLogic->getQrmRoom($hotelId, true);

        if (!empty($qrmRoom)) {
            $qrmRoomNumbers = array_column($qrmRoom, 'no');
            //判断房间号是否有重复
            $roomRepeat = array_count_values($qrmRoomNumbers);
            $qrmRoomLockType = array_column($qrmRoom, 'locktype');
            foreach ($rooms as &$roomItem) {
                if($roomRepeat[$roomItem['name']]<2){
                    $key = array_search($roomItem['name'], $qrmRoomNumbers);
                    if ( $key !== false) {
                        switch ($qrmRoomLockType[$key]) {
                            case '1':
                                $roomItem['from'] = 'qrm';
                                break;
                            case '2':
                                $roomItem['from'] = 'rf';
                                break;
                            case '3':
                                $roomItem['from'] = 'rfAndQrm';
                            break;
                            case '4':
                                $roomItem['from'] = 'other';
                            break;
                        }
                    $roomItem['masterentity_id'] = $qrmRoom[$key]['masterentity_id'];
                    $roomItem['masterbuildinfo_id'] = $qrmRoom[$key]['masterbuildinfo_id'];
                    $roomItem['masterfloorinfo_id'] = $qrmRoom[$key]['masterfloorinfo_id'];
                    $roomItem['masterroominfo_id'] = $qrmRoom[$key]['masterroominfo_id'];
                    }
                }
            }
            unset($roomItem);
        }
        $roomTypeId = $param['room'];
        $roomTypeName = $param['name'];
        $roomTypePrice = $param['price'];
        $roomTypeCharityPrice = isset($param['charityPrice']) ? $param['charityPrice'] : null;
       

        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomTypeLogic->editInfo($hotelId, $roomTypeId, $roomTypeName, $roomTypePrice, $rooms, null, $roomTypeCharityPrice);

        // 改价日志 义卖价
        if(isset($param['charityPrice']) && $roomTypeInfo['charityprice'] !== $param['charityPrice'] . ".00"){
            // 获取客栈默认符号
            $symbol = $this->getDefultCurrencyByHotel($hotelId);

            $lang = $this->priceLogMap[LANG_SET];
            $detail = $lang['price'] . $symbol . $roomTypeInfo['charityprice'] . $lang['change'] . $symbol . $param['charityPrice'];
            priceChangeLog($hotelId, $roomTypeId, $detail, strtotime(date('Y-m-d')), '', CHARITY_PRICE, '');
        }

        returnJson(200, 200, 'success');
    }

    /**
     * 删除房型
     * @modify trainyao 2016-01-04    创建
     */
    public function deleteRoom() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'room' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $roomTypeId = $param['room'];

        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomTypeLogic->deleteInfo($hotelId, $roomTypeId);

        returnJson(200, 200, 'success');
    }

    /**
     * 获取锁掌柜房间
     * @modify trainyao 2016-01-05    创建
     */
    public function getQrmRoom() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id'
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $roomLogic = A('Common/Room', 'Logic');
        $qrmRoomData = $roomLogic->getQrmRoom($hotelId);

        returnJson(200, 200, $qrmRoomData);
    }

    /**
     * 获取房型列表
     * @modify trainyao 2016-01-11    创建
     */
    public function getRoom() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomTypeInfo = $roomTypeLogic->getInfoByHotel($hotelId);

        // 处理数据
        $roomTypeInfo = array_values($roomTypeInfo);
        foreach ($roomTypeInfo as &$roomType) {
            unset($roomType['hotelentity_id']);
            unset($roomType['isdelete']);
            unset($roomType['price']);
            foreach ($roomType['room'] as &$room) {
                unset($room['hotelentity_id']);
                unset($room['floornumber']);
                unset($room['isdelete']);
                $room['type'] = $room['roomtype_id'];
                unset($room['roomtype_id']);
                unset($room['roomfrom']);
                unset($room['layout']);
                $room['dirty'] = $room['isdirty'];
                unset($room['isdirty']);
            }
            unset($room);
        }
        unset($roomType);

        returnJson(200, 200, $roomTypeInfo);
    }

    /**
     * 设置房间为脏房
     * @modify trainyao 2016-01-07 创建
     */
    public function setDirty() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'room' => 'notnull,notempty,concatId',
            'status' => 'notnull,notempty,roomDirty',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $roomId = explode(',', $param['room']);

        // 检查房间是否都可操作
        $roomLogic = A('Common/Room', 'Logic');
        $roomInfo = $roomLogic->getRoom($hotelId, $roomId);

        if (empty($roomInfo)) {
            returnJson(403, 202, 'error');
        }

        $roomId = array_column($roomInfo, 'id');

        $status = $param['status'];
        $timeStamp = $roomLogic->setDirty($hotelId, $roomId, ($status == 'dirty' ? ROOM_DIRTY : ROOM_CLEAN));

        returnJson(200, 200, $timeStamp);
    }

    /**
     * 同步单个锁掌柜房间
     * @modify trainyao 2016-04-25 创建
     */
    public function synRoom() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'roomId' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        // TOOD:添加权限校验,暂时只有超级管理员可以访问
        // 同步房间
        $hotelId = $param['hotel'];
        $roomId = $param['roomId'];
        $roomLogic = A('Common/Room', 'Logic');
        $synResult = $roomLogic->synRoom($hotelId, $roomId);
        if (empty($synResult)) {
            return;
        }
        returnJson(200, 200);
    }


    /**
     * 获取价格日历
     * @add Paddy 2016-11-25 创建
     */
    public function getPriceList() {
        $param = I('post.');
        $validateRule = array(
            'roomtype_id' => 'notnull,notempty,id'
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403,212,null);
        }
        
        $roomTypeId = $param['roomtype_id'];
        
        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
        
        if (empty($roomTypeInfo)) {
            returnJson(500, 504,null);
        }
        
        $startTime = strtotime(date('Y-m-1'));
        $fillDays = date('w', $startTime) - 1;
        if($fillDays==-1){
            $fillDays = 6;
        }
        $startTime = $startTime - $fillDays*86400;
        
        $endTime = strtotime(date('Y-m-1', strtotime('+6 month')))-86400;
        $fillDays = 7 - date('w', $endTime);
        if($fillDays==7){
            $fillDays = 0;
        }
        $endTime = $endTime + $fillDays*86400;
        
        $roomTypePriceModel = D('RoomTypePrice');
        $roomTypePriceList = $roomTypePriceModel->getPriceList($roomTypeId, $startTime, $endTime);
        if (!empty($roomTypePriceList)) {
            $roomTypePriceList = getArrayValueAsIndex($roomTypePriceList,'price_date');
        }

        //尾房价格
        $lastRoomPriceModel = D('LastRoomPrice');
        $nowadays = strtotime(date('Y-m-d'));
        $lastRoomPriceInfo = $lastRoomPriceModel->getInfoByRoomType($roomTypeId, $nowadays);
        if($lastRoomPriceInfo){
            $howManyDays = ($lastRoomPriceInfo['enddate'] - $nowadays)/86400;

            $startDate = $lastRoomPriceInfo['startdate'];
            $endDate = $lastRoomPriceInfo['enddate'];
            $startToEnd = ($endDate - $startDate)/86400;

            $startTime = $lastRoomPriceInfo['starttime'];
            //结束时间是0点 就转为24点，方便比较
            $endTime = $lastRoomPriceInfo['endtime'];
            $currentTime = date('G');

            if($startToEnd > 0 && $startTime > $endTime ){
                $tempTime = 0;
            }
            for ($i = $startDate; $i <= $endDate; $i += 86400) {
                //跨天数
                if(isset($tempTime) && $i == $nowadays){
                    if( $startTime <= $currentTime && $currentTime < 24 || $tempTime < $endTime && $endTime > $currentTime){
                        $hasLastRoom = true;
                    }
                }elseif ($startTime <= $currentTime && $currentTime < $endTime && $i == $nowadays) {
                    $hasLastRoom = true;
                }

                if($hasLastRoom){
                    if($roomTypePriceList[$i]['price']){
                        $roomTypePriceList[$i]['shopPrice'] = $roomTypePriceList[$i]['price'];
                        $roomTypePriceList[$i]['price'] -= $lastRoomPriceInfo['price'];
                    }else{
                        $roomTypePriceList[$i]['shopPrice'] = $roomTypeInfo['price'];
                        $roomTypePriceList[$i]['price'] = $roomTypeInfo['price'] - $lastRoomPriceInfo['price'];
                    }
                    $hasLastRoom = false; 
                }else{
                    $roomTypePriceList[$i]['price'] =  $roomTypePriceList[$i]['price'] ? $roomTypePriceList[$i]['price'] : $roomTypeInfo['price'];
                    $roomTypePriceList[$i]['shopPrice'] = $roomTypePriceList[$i]['price'];//门市价
                }

                $roomTypePriceList[$i]['start'] = date('Y-m-d H:i', $i + $startTime*3600);
                if($i == $endDate && isset($tempTime)){
                    $roomTypePriceList[$i]['end'] = date('Y-m-d H:i', $endDate+86400);
                }elseif(isset($tempTime)){
                    $roomTypePriceList[$i]['end'] = date('Y-m-d H:i', $i + 86400 + $endTime*3600);
                }else{
                    $roomTypePriceList[$i]['end'] = date('Y-m-d H:i', $i + $endTime*3600);
                }
                $roomTypePriceList[$i]['decPrice'] = $lastRoomPriceInfo['price'];
                $roomTypePriceList[$i]['price_date'] = $i;
                $roomTypePriceList[$i]['roomtype_id'] = $roomTypeId;
                $roomTypePriceList[$i]['last'] = '1';
            }
            
        }

        $roomTypeInfo['price_list'] = $roomTypePriceList ? $roomTypePriceList : [];
        
        returnJson(200, 200, $roomTypeInfo);
    }    

    /**
     * 预设价格
     * @add Paddy 2016-11-25 创建
     */
    public function setPrice() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'roomtype_id' => 'notnull,notempty,id',
            'price_date' => 'notnull,notempty,id',
            'price' => 'notnull,notempty,decimal8_2',
            
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403,212,null);
        }

        $hotel = $param['hotel'];
        $roomTypeId = $param['roomtype_id'];
        $date = $param['price_date'];
        $price = $param['price'];

        $lastRoomPriceModel = D('LastRoomPrice');
        $nowadays = strtotime(date('Y-m-d'));
        $lastRoomPriceInfo = $lastRoomPriceModel->getInfoByRoomTypeBetween($roomTypeId, $date);
        if($lastRoomPriceInfo['price']>$price && $date <= $lastRoomPriceInfo['enddate']){
            returnJson(403,310,null);
        }

        // 价格设置:不能低于义卖价
        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
        if (isset($lastRoomPriceInfo['price']) && $price - $lastRoomPriceInfo['price'] <= $roomTypeInfo['charityprice']) {
            returnJson(403,426,null);
        } else if ($price <= $roomTypeInfo['charityprice']) {
            returnJson(403,426,null);
        }

        // 价格设置:不能低于立减优惠金额
        if ($roomTypeInfo['minustype'] == 1 ) {
            if (isset($lastRoomPriceInfo['price']) && ($price - $lastRoomPriceInfo['price'] - $roomTypeInfo['charityprice']) <= $roomTypeInfo['minusdiscount']) {
                returnJson(403,312, 'minusdiscount error');
            }  else if ($price -  $roomTypeInfo['charityprice'] <= $roomTypeInfo['minusdiscount']) {
                returnJson(403,312, 'minusdiscount error');
            }
        }
        

        $startTime = strtotime(date('Y-m-1'));
        $fillDays = date('w', $startTime) - 1;
        if($fillDays==-1){
            $fillDays = 6;
        }
        $startTime = $startTime - $fillDays*86400;
        
        $endTime = strtotime(date('Y-m-1', strtotime('+6 month')))-86400;
        $fillDays = 7 - date('w', $endTime);
        if($fillDays==7){
            $fillDays = 0;
        }
        $endTime = $endTime + $fillDays*86400;
        
        if($date<$startTime || $date>$endTime){
            returnJson(403, 220, null);
        }
        $roomTypePriceModel = D('RoomTypePrice');
        $roomTypePrice = $roomTypePriceModel->getInfoByRoomType($roomTypeId,$date);
        $roomTypePriceModel->setPrice($roomTypeId,$date,$price);
        // 获取客栈默认符号
        $symbol = $this->getDefultCurrencyByHotel($hotel);
        $lang = $this->priceLogMap[LANG_SET];
        if($roomTypePrice){
            $detail = $lang['price'].$symbol.$roomTypePrice['price'].$lang['change'].$symbol.number_format($price, 2, '.', '');
        }else{
            $detail = $lang['set'].$symbol.number_format($price, 2, '.', '');
        }
        priceChangeLog($hotel, $roomTypeId, $detail, $date, '', RACK_PRICE, '');
        returnJson(200, 200, null);
    }
    /**
     * 获取预设价格 
     * @add Paddy 2016-12-1 创建
     * @add zhangz 2017-2-28 修改
     * @add zhangz 2017-5-26 修改
     * @modify wenhui 2017-11-30 添加维修房
     */
    public function getPriceByHotelId(){
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'date' => 'notnull,notempty,isDate'
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403, 212, null);
        }
        $hotelId = $param['hotel'];
        $date = $param['date'];
        $dateTime = strtotime(date('Y-m-d', strtotime($date)));
        
        $startTime = $dateTime - 3 * 86400;
        $endTime = $dateTime + 63 * 86400;
        
        $roomLogic = A('Common/Room', 'Logic');
        $priceInfo = $roomLogic->getRoomPrice($hotelId, $startTime, $endTime);
        
        returnJson(200, 200, $priceInfo);
    }
    /**
     * 获取特卖价格
     * @add zhangzhen 2017-2-27 创建
     */
    public function getLastRoomRrice(){
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'roomTypeId' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403, 212, null);
        }

        $hotelId = $param['hotel'];
        $roomTypeId = $param['roomTypeId'];
        $lastRoomPriceModel = D('LastRoomPrice');
        $nowadays = strtotime(date('Y-m-d'));
        $result = $lastRoomPriceModel->getInfoByRoomType($roomTypeId,$nowadays);
        if($result){
            $result['startdate'] = date('Y-m-d',$result['startdate']);
            $result['enddate'] = date('Y-m-d',$result['enddate']);
        }
        returnJson(200, 200, $result);
    }
    /**
     * 添加尾房
     * @add zhangzhen 2017-2-27 创建
     */
    public function addLastRoomRrice(){
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'roomTypeId' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty',
            'endDate' => 'notnull,notempty',
            'startTime' => 'notnull,notempty,isHour_G',
            'endTime' => 'notnull,notempty,isHour_G',
            'price' => 'notnull,notempty,natureNumber',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403, 212, null);
        }
        $hotelId = $param['hotel'];
        $roomTypeId = $param['roomTypeId'];
        $startDate = strtotime($param['startDate']);
        $endDate = strtotime($param['endDate']);
        $startTime = $param['startTime'];
        $endTime = $param['endTime'];
        $price = $param['price'];

        // 尾房特卖预定立减:不能低于义卖价
        $roomTypeModel = D('RoomType');
        $roomTypeInfo = $roomTypeModel->getInfo($roomTypeId);
        if ($roomTypeInfo['price'] - $price <= $roomTypeInfo['charityprice']) {
            returnJson(403, 426);
        }

        // 尾房特卖预定立减:不能低于立减优惠金额
        if ($roomTypeInfo['minustype'] == 1 && ($roomTypeInfo['price'] - $price - $roomTypeInfo['charityprice']) <= $roomTypeInfo['minusdiscount']) {
            returnJson(403, 313, 'minusdiscount error');
        }

        $lastRoomPriceModel = D('LastRoomPrice');
        if($param['id']){
            $lastRoomPrice = $lastRoomPriceModel->getInfoById($param['id']);
            $result = $lastRoomPriceModel->saveInfo($param['id'], $roomTypeId, $startDate, $endDate, $startTime, $endTime, $price);
        }else{
            $result = $lastRoomPriceModel->addInfo($roomTypeId, $startDate, $endDate, $startTime, $endTime, $price);
        }
        if($lastRoomPrice['price'] !== $price.".00"){
            // 获取客栈默认符号
            $symbol = $this->getDefultCurrencyByHotel($hotelId);
            $lang = $this->priceLogMap[LANG_SET];
            $detail = $lang['dec'].$symbol.$price;
            priceChangeLog($hotelId, $roomTypeId, $detail, $startDate, $endDate, LAST_PRICE, '');
        }
        returnJson(200, 200, $result);
    }
    /**
     * 删除特卖价格(软删除)
     * @add zhangzhen 2017-2-27 创建
     */
    public function delLastRoomRrice(){
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'id' =>'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403, 212, null);
        }
        $hotelId = $param['hotel'];
        $id = $param['id'];
        $lastRoomPriceModel = D('LastRoomPrice');
        $lasteRoomInfo = $lastRoomPriceModel->getInfoById($id);
        $result = $lastRoomPriceModel->delInfo($id);
        // 获取客栈默认符号
        $symbol = $this->getDefultCurrencyByHotel($hotelId);
        $lang = $this->priceLogMap[LANG_SET];
        priceChangeLog($hotelId, $lasteRoomInfo['roomtype_id'], $lang['clean'].$symbol . floatval($lasteRoomInfo['price']), $lasteRoomInfo['startdate'], $lasteRoomInfo['enddate'], LAST_PRICE, '');
        returnJson(200, 200, $result);
    }

    /**
     * 设置房间为维修房
     * @modify wenhui 2017-12-01 创建
     */
    public function setService() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'roomTypeId' => 'notnull,notempty,id',
            'roomId' => 'notnull,notempty,id',
            'status' => 'notnull,notempty,id',
        );

        if (isset($param['startDate']) && isset($param['endDate']) || $param['status'] == 1) {
            $validateRule['startDate'] = 'notnull,notempty,isDate';
            $validateRule['endDate'] = 'notnull,notempty,isDate';
        }

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $roomTypeId = $param['roomTypeId'];
        $roomId = $param['roomId'];
        $status = $param['status'];
        $startDate = isset($param['startDate']) ? $param['startDate'] : null;
        $endDate = isset($param['endDate']) ? $param['endDate'] : null;

        // 检查房间是否都可操作
        $roomLogic = A('Common/Room', 'Logic');
        $roomInfo = $roomLogic->getRoom($hotelId, $roomId);

        if (empty($roomInfo)) {
            returnJson(403, 202, 'error');
        }

        $setResult = $roomLogic->setService($hotelId, $roomTypeId, $roomId, $status, $startDate, $endDate);

        returnJson(200, 200);
    }
}
